//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package utils

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/org_dao"
)

func CheckIncludeDepartMent(ctx context.Context, orgId, departMentId, parentDepartMentId string) (bool, error) {
	if departMentId == parentDepartMentId {
		return true, nil
	}
	departMentItemList, err := org_dao.DepartMentDao.ListByOrg(ctx, orgId)
	if err != nil {
		return false, err
	}
	departMentItemMap := map[string]*org_dao.DepartMentInfoDbItem{}
	for _, departMentItem := range departMentItemList {
		departMentItemMap[departMentItem.DepartMentId] = departMentItem
	}
	tmpItem := departMentItemMap[departMentId]
	for {
		if tmpItem == nil {
			break
		}
		if tmpItem.ParentDepartMentId == parentDepartMentId {
			return true, nil
		}
		tmpItem = departMentItemMap[tmpItem.ParentDepartMentId]
	}
	return false, nil
}
